package com.zzyl.controller.manager.common;

import com.zzyl.base.ResponseResult;
import com.zzyl.file.FileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.UUID;

@RestController
@RequestMapping("/common")
@Slf4j
@Api(tags = "通用接口")
@RequiredArgsConstructor
public class CommonController {
	
	private final FileStorageService fileStorageService;
	
	/**
	 * 文件上传
	 * @param file 文件
	 * @return 上传结果
	 * @throws Exception 异常
	 */
	@PostMapping("/upload")
	@ApiOperation("文件上传")
	public ResponseResult<String> upload(
			@ApiParam(value = "上传的文件", required = true)
			@RequestPart("file") MultipartFile file) throws Exception {
		
		// 校验是否为图片文件
		try {
			BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
		} catch (Exception e) {
			throw new RuntimeException("上传图片失败");
		}
		
		if (file.getSize() == 0) {
			throw new RuntimeException("上传图片不能为空");
		}
		// 获得原始文件名
		String originalFilename = file.getOriginalFilename();
		// 获得文件扩展名
		String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
		String fileName = UUID.randomUUID().toString() + extension;
		
		String filePath = fileStorageService.store(fileName, file.getInputStream());
		
		return ResponseResult.success(filePath);
	}
	
}
